package com.chinasoftinc.facommon.view.login.abilitypresent;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.event.commonevent.MatchingSkills;
import ohos.global.configuration.Configuration;
import ohos.rpc.RemoteException;

import com.chinasoftinc.facommon.view.login.LoginAbility;
import com.chinasoftinc.facommon.FAConfigMgr;
import com.chinasoftinc.facommon.HilinkRegister;
import com.chinasoftinc.facommon.utils.LogUtil;

/**
 * @since 2022-05-09
 */
public abstract class LoginAbilityPresent {
    public static final String KEY_DEVICE_ID = "deviceId";
    public static final String KEY_SESSION_ID = "sessionId";
    public static final String KEY_PRODUCT_ID = "pid";
    public static final String KEY_UUID = "uuid";
    public static final String KEY_PRODUCT_INFO = "productInfo";

    private static final int WINDOW_MODAL = 3;
    private static final int PRODUCT_INFO_START = 0;
    private static final int PRODUCT_INFO_MIDDLE = 4;
    private static final int PRODUCT_INFO_END = 6;
    private static final int CONFIG_COMPLETE = 1;
    public static final String ACTION_CLOSE = "entry.fa.close";
    protected final String TAG = getClass().getSimpleName();

    /**
     * Location permission string
     */
    protected static final String PERM_LOCATION = "ohos.permission.LOCATION";

    protected final LoginAbility ability;
    private EventSubscriber subscriber;

    protected String deviceId;
    protected String productId;
    protected String uuid;
    protected String productInfo;
    protected String subModelId;

    public LoginAbilityPresent(LoginAbility ability) {
        this.ability = ability;
    }

    public void onStart(Intent intent) {
        deviceId = intent.getStringParam(KEY_DEVICE_ID);
        uuid = intent.getStringParam(KEY_UUID);
        productInfo = intent.getStringParam(KEY_PRODUCT_INFO);
        productId = (productInfo != null && productInfo.length() >= PRODUCT_INFO_MIDDLE)
            ? productInfo.substring(PRODUCT_INFO_START, PRODUCT_INFO_MIDDLE) : null;
        subModelId = (productInfo != null && productInfo.length() >= PRODUCT_INFO_END)
            ? productInfo.substring(PRODUCT_INFO_MIDDLE, PRODUCT_INFO_END) : null;

        LogUtil.info(TAG, "onStart deviceId: " + deviceId);
        LogUtil.info(TAG, "onStart uuid: " + uuid);
        LogUtil.info(TAG, "onStart productId: " + productId);
        LogUtil.info(TAG, "onStart subModelId: " + subModelId);

        subscriber = subscribeEvent();
        try {
            CommonEventManager.subscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        HilinkRegister.get().configInfo(productId, uuid, deviceId);
    }


    protected void configPageParams() {
        int systemColorMode = ability.getResourceManager().getConfiguration().getSystemColorMode();
        IntentParams params = new IntentParams();
        params.setParam("isLight", systemColorMode == Configuration.LIGHT_MODE);
        params.setParam(KEY_DEVICE_ID, deviceId);
        params.setParam(KEY_PRODUCT_ID, productId);
        params.setParam(KEY_UUID, uuid);
        LogUtil.info(TAG, "systemColorMode : " + systemColorMode);
        ability.setPageParams(null, params);
    }

    public abstract void startAbility();

    public void onStop() {
        try {
            CommonEventManager.unsubscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            LogUtil.error(TAG, "unsubscribeCommonEvent error");
        }
    }

    private EventSubscriber subscribeEvent() {
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(ACTION_CLOSE);
        addSubscriberEvent(matchingSkills);
        return new EventSubscriber(new CommonEventSubscribeInfo(matchingSkills));
    }

    protected void addSubscriberEvent(MatchingSkills matchingSkills) {
    }

    protected void onReceiveEvent(String action) {
    }

    /**
     * 接收关闭FA的公共事件
     *
     * @since 2022-01-25
     */
    protected class EventSubscriber extends CommonEventSubscriber {
        private EventSubscriber(CommonEventSubscribeInfo subscribeInfo) {
            super(subscribeInfo);
        }

        @Override
        public void onReceiveEvent(CommonEventData commonEventData) {
            Intent intent = commonEventData.getIntent();
            String action = intent.getAction();
            LogUtil.info(TAG, "onReceiveEvent, action: " + action);
            if (action == null) {
                return;
            }
            if (action.equals(ACTION_CLOSE)) {
                ability.terminateAbility();
            } else {
                LoginAbilityPresent.this.onReceiveEvent(action);
            }
        }
    }
}
